/**
 * 遮罩层控件
 * op:开关
 * z-index:层级，默认99
 */
$.showAddBg = function(op, zIndex, isLoading){
    if (op) {
    	if($("#BG_SHADE_DIV").length==0){
        	$("body").append("<div id=\"BG_SHADE_DIV\"></div>");
        	$(window).resize(function(){
				$("#BG_SHADE_DIV").css({
		            "height": $(window).height() + "px",
		            "width": $(window).width() + "px"
		       });
			});
    	} 
        $("#BG_SHADE_DIV").css({
            "height": $(document).height() + "px",
            "width": $(document).width() + "px",
            "zIndex": 99
        }).show();
        if (zIndex != null) {
            $("#BG_SHADE_DIV").css("zIndex", zIndex);
        }
        if(isLoading) {
        	$("#BG_SHADE_DIV").html("<div class=\"loading\">&nbsp</div>");
        }
    }
    else {
        $("#BG_SHADE_DIV").hide().empty();
    }
};

/**
 * 显示当前DIV的详细描述
 */
$.showShortTitle = function(id, title, length, key){
	if(title.length>length){
		return "<a class=\"edit\" onclick=\"$(this).showObjInfo('"+id+"', '"+key+"')\" >"+title.substr(0, (length-3))+"...</a>";
	} else {
		return "<a class=\"edit\" onclick=\"$(this).showObjInfo('"+id+"', '"+key+"')\" >"+title+"</a>";;
	}
};
$.fn.showObjInfo = function(objId, key){
	var content = eval('showDetail(' + objId + ', \''+key+'\')');
    var dom = $(this).parent();
    if ($("#FLOAT_DESC_DIV").length == 0) {
        $("body").append("<div id=\"FLOAT_DESC_DIV\" style=\"background:#ffffff; border:1px solid #a8c7ce; color:#008EE8; display:none; "
		+"padding:5px; overflow-x:auto; position:absolute; z-index:98; word-break:break-all; \"></div>");
    	var plugin = {};
    	plugin.descdiv = $("#FLOAT_DESC_DIV");
    	plugin._mousedown = function(e) {
			// e.preventDefault();
			if (plugin.descdiv.css('display') == 'block') {
				if (!$(e.target).is(plugin.descdiv)) {
					plugin.descdiv.slideUp("fast");
				}
			}
		};
		$(document).bind('mousedown', plugin._mousedown);
    }
    var targetDom = $("#FLOAT_DESC_DIV");
    if(targetDom.data("value")==(objId+"_"+key) && targetDom.css('display') == 'block') {
    	 targetDom.slideUp("fast");
    	 return;
    }
    targetDom.hide();
    targetDom.data("value", objId+"_"+key);
    targetDom.css({
	        "top": (dom.offset().top + dom.height()) + "px",
	        "left": (dom.offset().left-1) + "px",
	        "max-width": "380px"
	    }).html(content).slideDown("fast");
};
$.showShortCon = function(id, content, length, key){
	if(content.length>length){
		return content.substr(0, (length-3))+"...<a class=\"edit\" onclick=\"$(this).showDesc('"+id+"', '"+key+"')\" >[详细]</a>";
	} else {
		return content;
	}
};
$.fn.showDesc = function(objId, key){
	var content = eval('showDetail(' + objId + ', \''+key+'\')');
    var dom = $(this).parent();
    if ($("#FLOAT_DESC_DIV").length == 0) {
        $("body").append("<div id=\"FLOAT_DESC_DIV\" style=\"background:#ffffff; border:1px solid #a8c7ce; color:#008EE8; display:none; "
		+"padding:5px; overflow-x:auto; position:absolute; z-index:98; word-break:break-all; \"></div>");
    	var plugin = {};
    	plugin.descdiv = $("#FLOAT_DESC_DIV");
    	plugin._mousedown = function(e) {
			// e.preventDefault();
			if (plugin.descdiv.css('display') == 'block') {
				if (!$(e.target).is(plugin.descdiv)) {
					plugin.descdiv.slideUp("fast");
				}
			}
		};
		$(document).bind('mousedown', plugin._mousedown);
    }
    var targetDom = $("#FLOAT_DESC_DIV");
    if(targetDom.data("value")==(objId+"_"+key) && targetDom.css('display') == 'block') {
    	 targetDom.slideUp("fast");
    	 return;
    }
    targetDom.hide();
    targetDom.data("value", objId+"_"+key);
    targetDom.css({
        "top": (dom.offset().top + dom.height()) + "px",
        "left": (dom.offset().left-1) + "px",
        "width": (dom.outerWidth()-10)+"px"
    }).html(content).slideDown("fast");
};
$.fn.showStaticMap = function(lng, lat){
	if ($("#FLOAT_STATIC_MAP_DIV").length == 0) {
        $("body").append("<img id=\"FLOAT_STATIC_MAP_DIV\" width=\"300\" height=\"160\" style=\"background:#ffffff; position:absolute; z-index:98; display:none; \" />");
    	var plugin = {};
    	plugin.descdiv = $("#FLOAT_STATIC_MAP_DIV");
    	plugin._mousedown = function(e) {
			if (!plugin.descdiv.is(":hidden")) {
				if (!$(e.target).is(plugin.descdiv)) {
					plugin.descdiv.slideUp("fast");
				}
			}
		};
		$(document).bind('mousedown', plugin._mousedown);
    }
    var dom = $(this).parent();
    $("#FLOAT_STATIC_MAP_DIV").css({
        "top": (dom.offset().top + dom.height()+1) + "px",
        "left": (dom.offset().left) + "px"
    }).attr("src", "http://api.map.baidu.com/staticimage?width=300&height=160&markers="+lng+","+lat+"&zoom=15&markerStyles=l,,0x008EE8").slideDown("fast");
};
$.hideDesc = function(){
	$("#FLOAT_DESC_DIV").hide();
};
/**
 * 显示提示卡片
 * msg	提示的内容那个
 * btn	按钮的显示内容
 * fun	点击按钮额外触发的动作
 */
$.showAlert = function(msg, btn, fun){
	$.showAddBg(true, 99998);
	if($("#FLOAT_ALERT_DIV").length!=0) {
		$("#FLOAT_ALERT_DIV").remove();
	}
	$("body").append("<div id=\"FLOAT_ALERT_DIV\" class=\"divAlertCard\">"
			+"<span class=\"btn\">"+btn+"</span>"
			+"<span class=\"pin\"></span>"
			+"<span class=\"text\">"+msg+"</span></div>");
	var self = $("#FLOAT_ALERT_DIV");
	self.slideDown("fast").find("span.btn").bind("click", function(){
		self.slideUp("fast");
		$.showAddBg(false);
		if($.isFunction(fun)) {
			fun();
		}
	});
};

$.changeAlert = function(msg){
	if($("#FLOAT_ALERT_DIV").length!=0) {
		$("#FLOAT_ALERT_DIV").find("span.text").html(msg);
	}
};

$.hideAlert = function(){
	$.showAddBg(false);
	if($("#FLOAT_ALERT_DIV").length!=0) {
		$("#FLOAT_ALERT_DIV").slideUp("fast", function(){
			$(this).remove();
		});
	}
};

$.fn.toggleFrame = function(){
	var owner = $(this);
	owner.toggle(
		function () {
			owner.html("取消全屏");
			var leftFrame = window.parent;
			leftFrame.parent.document.getElementById("setFrame").rows="0,*,0";
			var tds = leftFrame.document.getElementsByTagName("td");
			tds[0].style.display = "none";
			tds[1].style.display = "none";
			tds[2].style.display = "none";
			tds[4].style.display = "none";
		},
		function () {
			owner.html("全屏查看");
			var leftFrame = window.parent;
			leftFrame.parent.document.getElementById("setFrame").rows="127,*,11";
			var tds = leftFrame.document.getElementsByTagName("td");
			tds[0].style.display = "";
			tds[1].style.display = "";
			tds[2].style.display = "";
			tds[4].style.display = "";
		}
	);
};

$.fn.showChoose = function(showList, valueList, clickFun){
	$.showAddBg(true, 99998);
	if($("#FLOAT_CHOOSE_DIV").length==0) {
		$("body").append("<div id=\"FLOAT_CHOOSE_DIV\" class=\"divRateCard\" style=\"height:20px; right:auto;\"></div>");
	}
	var html = "";
	for(var i=0; i<showList.length; i++) {
		html+= "<span class=\"btn\" onclick=\""+clickFun+"('"+valueList[i]+"');\">"+showList[i]+"</span>";
	}
	html+= "<span class=\"cancel\">取消</span>";
	var self = $("#FLOAT_CHOOSE_DIV");
	var position = $(this).offset();
	self.html(html).css({
		"left": (position.left-1)+"px",
		"top": (position.top+$(this).outerHeight())+"px"
	}).slideDown("fast");
	self.find("span").click(function(){
		self.slideUp("fast");
		$.showAddBg(false);
	});
};

/**
 * 显示进度条
 */
$.showUploadRate = function(parentDiv){
    var html = "<div id=\"" + parentDiv + "_rate\" style=\"border:1px solid #008EE8; float:left; height:20px; width:200px\">" +
    "<div id=\"" +
    parentDiv +
    "_bg\" style=\"background:#6495ED; float:left; height:100%; width:0%\">" +
    "<span id=\"" +
    parentDiv +
    "_val\" style=\"float:right; margin-right:-200px; line-height:20px;\">0%</span></div></div>";
    $("#" + parentDiv).html(html);
    $.startUpload(parentDiv, 10, 0);
};
$.startUpload = function(parentDiv, speed, rate) {
	var timer = null;
	timer = window.setInterval(function() {
		if(rate == 50) {
			window.clearInterval(timer);
			$.startUpload(parentDiv, 50, 51);
		} else if(rate == 98) {
			window.clearInterval(timer);
		}
		$("#" + parentDiv + "_bg").css("width", rate + "%");
		$("#" + parentDiv + "_val").css("margin-right", (rate * 2 - 200) + "px").text(rate + "%");
		rate++;
	}, speed);
}; 
$.endUpload = function(parentDiv){
    $("#" + parentDiv + "_bg").css("width", "100%");
    $("#" + parentDiv + "_val").css("margin-right", "0px").text("100%");
};
$.removeUpload = function(parentDiv){
    $("#" + parentDiv + "_rate").remove();
};

/**
 * 分页控件
 * funName:分页点击后调用的方法，参数必须为当前跳转页面
 * totalPage:总页数
 * curPage:当前页码
 */
$.fn.initPagination = function(funName, totalPage, curPage) {
    var PageHtml = "";
    var TotalPageNum = parseInt(totalPage, 10);
    var CurrentPageNum = parseInt(curPage, 10);
    if (TotalPageNum <= 10) {
    	for(var i=1; i<=TotalPageNum; i++) {
        	if(i==CurrentPageNum) {
            	PageHtml += "<a href=\"###\" class=\"active\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
        	} else {
            	PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
        	}
        }
    } else {
    	if(CurrentPageNum<=4){
    		for (var i = 1; i < 9; i++) {
            	if(i==CurrentPageNum) {
	            	PageHtml += "<a href=\"###\" class=\"active\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
	        	} else {
	            	PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
	        	}
            }
            PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+(CurrentPageNum+10>TotalPageNum?TotalPageNum:CurrentPageNum+10)+");\">...</a>";
    		PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+TotalPageNum+");\">&raquo;"+TotalPageNum+"</a>";
    	} else if(TotalPageNum-CurrentPageNum<=7) {
    		PageHtml += "<a href=\"###\" onclick=\"" + funName+"(1);\">1&laquo;</a>";
    		PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+(CurrentPageNum-10<1?1:CurrentPageNum-10)+");\">...</a>";
    		for (var i = (TotalPageNum-8); i <= TotalPageNum; i++) {
            	if(i==CurrentPageNum) {
	            	PageHtml += "<a href=\"###\" class=\"active\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
	        	} else {
	            	PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
	        	}
            }
    	} else {
    		PageHtml += "<a href=\"###\" onclick=\"" + funName+"(1);\">1&laquo;</a>";
    		PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+(CurrentPageNum-10<1?1:CurrentPageNum-10)+");\">...</a>";
    		for(var i=CurrentPageNum-2; i<=CurrentPageNum+5; i++){
    			if(i==CurrentPageNum) {
	            	PageHtml += "<a href=\"###\" class=\"active\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
	        	} else {
	            	PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+i+");\">" + i + "</a>";
	        	}
    		}
    		PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+(CurrentPageNum+10>TotalPageNum?TotalPageNum:CurrentPageNum+10)+");\">...</a>";
    		PageHtml += "<a href=\"###\" onclick=\"" + funName+"("+TotalPageNum+");\">&raquo;"+TotalPageNum+"</a>";
    	}
    }
    $(this).html(PageHtml);
};

$.fn.hour = function(value){
	var html = "";
	for(var i=0; i<24; i++){
		var temp = i;
		if(i<10) {
			temp = "0"+i;
		}
		html += "<option value=\""+temp+"\">"+temp+"</option>";
	}
	$(this).html(html).val(value);
};
$.fn.minute = function(value){
	var html = "";
	for(var i=0; i<60; i++){
		var temp = i;
		if(i<10) {
			temp = "0"+i;
		}
		html += "<option value=\""+temp+"\">"+temp+"</option>";
	}
	$(this).html(html).val(value);
};

$.formatObj = function(obj){
	for (var key in obj) {
		if(obj[key]==null || obj[key]=="null") {
			obj[key]="";
		}
	};
	return obj;
};






























